package classmate.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import classmate.dao.IRoomDao;
import classmate.dao.IStudentDao;
import classmate.dao.impl.RoomDaoImpl;
import classmate.dao.impl.StudentDaoImpl;
import classmate.po.Room;
import classmate.po.Student;

public class ClassmateService {
	private IStudentDao studentDao = new StudentDaoImpl();
	private IRoomDao roomDao = new RoomDaoImpl();
	
	/**
	 * 按照学生学号查询学生具体信息
	 * @param studentList
	 * @param student
	 * @return
	 */
	public List<Student> searchById(List<Student> studentList,Student student){
		List<Student> result = new ArrayList<Student>();
		Collections.sort(studentList);
		int m = -1;
		while((m = Collections.binarySearch(studentList, student,null))>=0) {
			Student stu = studentList.get(m);
			studentList.remove(m);
			result.add(stu);
		}
		return result;
	}
	/**
	 * 按照宿舍序号查询学生信息
	 * @param majorClassList
	 * @param majorClass
	 * @return
	 */
	public List<Room> searchByRoomID(List<Room> roomList, Room room){
		List<Room> result = new ArrayList<Room>();
		Collections.sort(roomList);
		int m = -1;
		while((m = Collections.binarySearch(roomList, room, null))>=0){
			Room mc = roomList.get(m);
			roomList.remove(m);
			result.add(mc);
		}
		return result;
	}
	/**
	 * 添加学生信息
	 * @param student
	 */
	public void addStudent(Student student) {
		studentDao.save(student);
	}
	
	/**
	 * 添加宿舍
	 * @param majorClass
	 */
	public void addRoom(Room room) {
		roomDao.save(room);
	}
	
	/**
	 * 更改学生信息
	 * @param student
	 */
	public void updateStudent(int id,Student student) {
		studentDao.update(student);
	}
	
	/**
	 * 更改宿舍信息
	 * @param majorClass
	 */
	public void updateRoom(Room room) {
		roomDao.update(room);
	}
	
	/**
	 * 删除学生信息
	 * @param id
	 */
	public void deleteStudent(long id) {
		studentDao.delete(id);
	}
	
	/**
	 * 删除宿舍信息
	 * @param id
	 */
	public void deleteRoom(int id) {
		roomDao.delete(id);
	}
	
	/**
	 *查询学生信息
	 * @param id
	 * @return
	 */
	public List<Student> getStudent(long id) {
		return studentDao.get(id);
	}
	/**查询宿舍信息
	 * @param id
	 * @return
	 */
	public Room getRoom(int id) {
		return roomDao.get(id);
	}
	
	/**
	 * 查询所有学生信息
	 * @return
	 */
	public List<Student> findAllStudent(){
		return studentDao.findAll();
	}
	/**查询所有宿舍信息
	 * @return
	 */
	public List<Room> findAllRoom(){
		return roomDao.findAll();
	}
}
